package com.hoshiicloud.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.goods.entity.spu.SpuPropertiesValues;
import com.hoshiicloud.goods.feign.IdGeneratorService;
import com.hoshiicloud.goods.mapper.spu.SpuPropertiesValuesMapper;
import com.hoshiicloud.goods.service.SpuPropertiesValuesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 商品属性组 服务实现类
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Slf4j
@Service
public class SpuPropertiesValuesServiceImpl extends ServiceImpl<SpuPropertiesValuesMapper, SpuPropertiesValues> implements SpuPropertiesValuesService {

    @Autowired
    private IdGeneratorService idGeneratorService;

    /**
     * @param id
     * @return SpuPropertiesValues
     * @description: 根据id查询商品属性组
     */
    @Override
    @Cacheable(cacheNames = "SpuPropertiesValues", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "商品管理-商品属性组-查询商品属性组 ")
    public SpuPropertiesValues getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
     * @param entity
     * @return boolean
     * @description: 新增商品属性组
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "SpuPropertiesValues", allEntries = true)
    @Logable(authName = "商品管理-商品属性组-新增商品属性组")
    public boolean save(SpuPropertiesValues entity) {

        // 主键ID生成
        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("主键ID生成失败"));
        }

        // 新增数据
        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
     * @param entity
     * @return boolean
     * @description: 修改商品属性组
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "SpuPropertiesValues", allEntries = true)
    @Logable(authName = "商品管理-商品属性组管理-修改商品属性组")
    public boolean updateById(SpuPropertiesValues entity) {

        // 更新数据
        entity.setUpdatedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    /**
     * @param queryWrapper 查询条件
     * @return IPage
     * @description: 分页查询商品属性组
     */
    @Override
    @Logable(authName = "商品管理-商品属性组管理-分页查询商品属性组")
    @Cacheable(cacheNames = "SpuPropertiesValues", key = "T(com.hoshiicloud.common.utils.DigestUtils).md5(#root.targetClass.simpleName+':'+#root.methodName+':'+#key)")
    public IPage getPage(IPage page, Wrapper queryWrapper, String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "SpuPropertiesValues", allEntries = true)
    @Logable(authName = "商品管理-商品属性组管理-删除商品属性组")
    public boolean deleteById(Long id) {

        // 根据id查询数据
        SpuPropertiesValues entity = this.getById(id);
        if (entity == null) {
            log.error("删除商品属性组异常，无法找到商品属性组记录");
            throw new ServiceException("删除商品属性组异常，无法找到商品属性组记录");
        }

        //进行删除操作
        entity.setUpdatedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }

    @Override
    @Cacheable(cacheNames = "SpuPropertiesValues", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "商品管理-商品属性组管理-根据id集合查询商品属性组")
    public List<SpuPropertiesValues> getByIds(List<Long> ids, String key) {
        return baseMapper.selectBatchIds(ids);
    }
}
